package case1.composite;

import case1.Quackable;
import case1.core.observer.Observable;
import case1.core.observer.Observer;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

/**
 * description:
 * 鸭子集群
 *
 * @author Yuanml on 2020/10/30 14:57
 */
public class Flock implements Quackable {
    private final Observable observable;
    private final List<Quackable> quackers = new ArrayList<>();

    public Flock() {
        observable = new Observable(this);
    }

    public void add(Quackable quacker) {
        quackers.add(quacker);
    }

    @Override
    public void quack() {
        //迭代器模式
        Iterator<Quackable> iterator = quackers.iterator();
        while (iterator.hasNext()) {
            Quackable next = iterator.next();
            next.quack();
        }
        notifyObserver();
    }

    @Override
    public void registerObserver(Observer observer) {
        observable.registerObserver(observer);
    }

    @Override
    public void notifyObserver() {
        observable.notifyObserver();
    }
}
